package com.yupi.springbootinit.bizmq;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.yupi.springbootinit.constant.BiMqConstant;

import java.util.HashMap;
import java.util.Map;

/**
 * 用于创建程序用到的交换机和队列（只用在程序启动前执行一次）
 * @author 穹龙
 * @date 2025/2/10
 */
public class BiInitMain {
    public static void main(String[] args) {
        try {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("localhost");
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
            String EXCHANGE_NAME = BiMqConstant.BI_EXCHANGE_NAME;
            channel.exchangeDeclare(EXCHANGE_NAME, "direct");

            String queueName = BiMqConstant.BI_QUEUE_NAME;
            // 指定消息过期参数
            Map<String, Object> channelArgs = new HashMap<>();
            // 设置5秒过期
            channelArgs.put("x-message-ttl", 5000);
            // 设置一次只能处理一条消息
            channel.basicQos(1);
            channel.queueDeclare(queueName, true, false, false, channelArgs);
            channel.queueBind(queueName, EXCHANGE_NAME, BiMqConstant.BI_ROUTING_KEY);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
